'use strict';

angular.module('app')
.controller('dicUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster',
    function($scope, $rootScope, $state, $timeout, toaster) {
        $scope.record = {
        	id : $state.params.id,
        	editable_ : 1,
        	enable_:1
        }
        if($state.includes('**.dic.update')){
            $scope.loading = true;
            $scope.reset = false;//修改不允许重置
            $scope.title="编辑字典";
            var id = $state.params.id;
            activate(id);
        }else if($state.includes('**.dic.create')){
            $scope.title="添加字典";
            $scope.reset = true;//新增时可以重置
        }
        validate();
      	//重置表单
		$scope.resetFrom = function(){
		    $scope.record = {
	        	id : $state.params.id,
	        	editable_ : 1,
	        	enable_:1
        	}
		}
        $scope.submit= function(){
            var m = $scope.record;
            if($state.params.id != null){//id非空代表着是修改
            	if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8',
        				url : '/dic/update',
        				data: angular.toJson($scope.record)
        			}).then(function(result){
        				if(result.httpCode ==200){//成功
	                        toaster.clear('*');
	                        toaster.pop('success', '', "保存成功");
	                        $timeout(function(){
	                            $state.go('main.sys.dic.list');
	                        },2000);
	                    }else{
	                        toaster.clear('*');
	                        toaster.pop('error', '', result.msg);
	                        $scope.isDisabled = false;
	                    }
        			});
                }
            }else{//id为空代表着是新增
            	if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8',
        				url : '/dic/create',
        				data: angular.toJson($scope.record)
        			}).then(function(result){
        				if(result.httpCode ==200){//成功
	                        toaster.clear('*');
	                        toaster.pop('success', '', "保存成功");
	                        $timeout(function(){
	                            $state.go('main.sys.dic.list');
	                        },2000);
	                    }else{
	                        toaster.clear('*');
	                        toaster.pop('error', '', result.msg);
	                        $scope.isDisabled = false;
	                    }
        			});
                }
            }
        }

        // 初始化页面
        function activate(id) {
        	$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dic/queryByDicId',
				data: angular.toJson({'id': id})
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.record = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
        }

        //表单验证
        function validate(){
            jQuery('form').validate({
                rules: {
                	type_: {
                        required: true
                    },
                    code_text: {
                        required: true
                    },
                    code_: {
                        required: true
                    },
                    sortNo: {
                    	maxLengthB:[2]
                    }
                },
                messages: {
                	type_: {
                        required: '请输入字典类型'
                    },
                    code_text: {
                        required: '请输入字典名称'
                    },
                    code_: {
                        required: '请输入关键字'
                    },
                    sortNo: {
                    	maxLengthB: '请输入一位或两位数字'
                    }
                },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }

    }]);